﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Twitterizer;

namespace Twillo.Core.Controls
{
    public class TwitterStatusLabel : FlowLayoutPanel
    {
        [Category("Twitter")]
        public TwitterStatus TwitterStatus { get { return _status; } set { _status = value; RefreshControl(); } }

        [Category("Events")]
        public event LinkClickedHandler LinkClicked;

        private TwitterStatus _status = null;

        private const string regexpattern = "(^|[\\n ])(?<url>[\\w]+?://[\\w]+[^ \\\"\\n\\r\\t< ]*)|(^|[\\n ])(?<url>(www|ftp)\\.[^ \\\"\\t\\n\\r< ]*)|\\@(?<user>\\w+)|\\#(?<keyword>\\w+)";

        private void RefreshControl()
        {
            this.Controls.Clear();
            if (_status == null) { return; }

            string _text = _status.Text;

            MatchCollection matchs = Regex.Matches(_text, regexpattern, System.Text.RegularExpressions.RegexOptions.Singleline);
            string currentText = string.Empty;
            int start = 0;
            this.Controls.Clear();
            this.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;
            this.WrapContents = true;
            this.Padding = new Padding(0);
            this.Margin = new Padding(0);

            foreach (Match m in matchs)
            {
                if (m.Groups["user"].Success || m.Groups["keyword"].Success || m.Groups["url"].Success)
                {
                    currentText = _text.Substring(start, m.Index - start);
                    Label lbl = new Label();
                    lbl.Text = currentText.Trim();
                    lbl.AutoSize = true;
                    currentText = string.Empty;
                    start = m.Index + m.Length;
                    LinkLabel linklbl = new LinkLabel();
                    linklbl.AutoSize = true;
                    lbl.Margin = new System.Windows.Forms.Padding(0);
                    linklbl.Margin = new System.Windows.Forms.Padding(0);
                    lbl.Padding = new System.Windows.Forms.Padding(0);
                    linklbl.Margin = new System.Windows.Forms.Padding();
                    linklbl.Text = m.Value.Trim();
                    linklbl.Click += new EventHandler(LinkLabel_Click);
                    linklbl.Font = this.Font;
                    lbl.Font = this.Font;
                    linklbl.BackColor = this.BackColor;
                    lbl.BackColor = this.BackColor;
                    linklbl.ForeColor = this.ForeColor;
                    lbl.ForeColor = this.ForeColor;

                    if (m.Groups["user"].Success) { linklbl.Tag = "user$" + m.Groups["user"].Value.Trim(); }
                    if (m.Groups["keyword"].Success) { linklbl.Tag = "keyword$" + m.Groups["keyword"].Value.Trim(); }
                    if (m.Groups["url"].Success) { linklbl.Tag = "url$" + m.Groups["url"].Value.Trim(); }

                    this.Controls.Add(lbl);
                    this.Controls.Add(linklbl);
                }
            }

            if (start < _text.Length)
            {
                currentText = _text.Substring(start, _text.Length - start);
                Label lbl = new Label();
                lbl.Text = currentText.Trim();
                lbl.AutoSize = true;
                currentText = string.Empty;
                lbl.Margin = new System.Windows.Forms.Padding(0);
                lbl.Padding = new System.Windows.Forms.Padding(0);
                lbl.Font = this.Font;
                lbl.BackColor = this.BackColor;
                lbl.ForeColor = this.ForeColor;
                this.Controls.Add(lbl);
            }
        }

        protected void LinkLabel_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = sender as LinkLabel;
            string[] args = lbl.Tag.ToString().Split('$');
            TwitterStatusLabelClickEventArgs ev = null;

            switch (args[0])
            {
                case "user":
                    ev = new TwitterStatusLabelClickEventArgs(args[1], TwitterStatusLabelLinkType.User);
                    break;
                case "keyword":
                    ev = new TwitterStatusLabelClickEventArgs(args[1], TwitterStatusLabelLinkType.Keyword);
                    break;
                case "url":
                    ev = new TwitterStatusLabelClickEventArgs(args[1], TwitterStatusLabelLinkType.Url);
                    break;
            }

            if (ev != null)
            {
                if (LinkClicked != null) { LinkClicked(this, ev); }
            }
        }

        public delegate void LinkClickedHandler(object sender, TwitterStatusLabelClickEventArgs e);

    }

    public class TwitterStatusLabelClickEventArgs : EventArgs
    {
        public string Data { get { return _data; } }
        public TwitterStatusLabelLinkType LinkType { get { return _linktype; } }

        private string _data = string.Empty;
        private TwitterStatusLabelLinkType _linktype = TwitterStatusLabelLinkType.Keyword;

        public TwitterStatusLabelClickEventArgs(string data, TwitterStatusLabelLinkType linktype)
        {
            _data = data;
            _linktype = linktype;
        }
    }

    public enum TwitterStatusLabelLinkType
    {
        User,
        Keyword,
        Url
    }

}
